
package com.zwh;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

import nl.siegmann.epublib.domain.Author;
import nl.siegmann.epublib.domain.Book;
import nl.siegmann.epublib.domain.Date;
import nl.siegmann.epublib.domain.Identifier;
import nl.siegmann.epublib.domain.Metadata;
import nl.siegmann.epublib.domain.Resource;
import nl.siegmann.epublib.domain.Resources;
import nl.siegmann.epublib.domain.Spine;
import nl.siegmann.epublib.domain.SpineReference;
import nl.siegmann.epublib.domain.TOCReference;
import nl.siegmann.epublib.domain.TableOfContents;
import nl.siegmann.epublib.epub.EpubReader;

public class EpubUtil {

	public static void main(String[] args) {
		String pf = "xxx.epub";
		String pfImg = "test.jpg";
		String curPath = "test";

		
		EpubReader epubReader = new EpubReader();
		try {
			Book book = epubReader.readEpub(new FileInputStream(pf));

			Metadata metadata = book.getMetadata();
			List<String> titles = metadata.getTitles();
			System.out.println("book title:" + (titles.isEmpty() ? "book has no title" : titles.get(0)));

			List<Author> authors = metadata.getAuthors();
			System.out.println("authors" + authors);

			List<String> publishers = metadata.getPublishers();
			System.out.println("publishers" + publishers);

			//简介
			List<String> descriptions = metadata.getDescriptions();
			System.out.println("descriptions" + descriptions);

			List<Date> dates = metadata.getDates();
			//出版时间
			String publishTime = "";
			for (Date date : dates) {
				if (date.getEvent().toString().equals("publication")) {
					publishTime = date.getValue();
					break;
				}
				
				publishTime = date.getValue();
			}

			List<Identifier> identifiers = metadata.getIdentifiers();
			String isbn ="";
			for(Identifier identifier:identifiers){
				if(identifier.getScheme().equals("ISBN")){
					isbn = identifier.getValue();
				}
			}
			//封面
			Resource coverImage = book.getCoverImage();
			File myFile = new File(pfImg);
			if(!myFile.exists()){
				int index = pfImg.lastIndexOf("/")+1;
				File dir = new File(pfImg.substring(0,index));
				if(dir.mkdirs()){
					System.out.println("创建目录成功");
				}
				//myFile.createNewFile();
			}
			//System.out.println(coverImage.getMediaType().getName()+coverImage.getMediaType().getDefaultExtension());
			if (coverImage != null) {
				FileOutputStream os = new FileOutputStream(pfImg);
				os.write(coverImage.getData());
			} else {
				System.err.println("cover not found");
			}

			File file4 = new File(curPath + "\\tempBook4.html");

			FileOutputStream outputStream = new FileOutputStream(file4);

			TableOfContents tb = book.getTableOfContents();
			List<TOCReference> tocReferences = tb.getTocReferences();
			System.out.println("总章节数" + tocReferences.size());
			Spine spine = book.getSpine();
			List<SpineReference> spineReferences = spine.getSpineReferences();
			System.out.println("总内容数" + spineReferences.size());

			Resources resources = book.getResources();
			//			System.out.println(resources.getTitle() + resources.getHref());
			//			InputStream inputStream = resources.getInputStream();

			//			byte[] buffer = new byte[512];
			//			int off = 0;
			//			int len = 512;
			//			while (inputStream.read(buffer, off, len) != -1) {
			//				outputStream.write(buffer, off, len);
			//			}
			//			outputStream.close();
			//			inputStream.close();
			//			System.out.println("完成");
			//writer3.flush();
			/*int i = 0;
			for (TOCReference tocReference : tocReferences) {
				i++;
				//System.out.println(tocReference.getTitle() + tocReference.getCompleteHref());//章节标题
				File file3 = new File(curPath + "\\tempBook" + i + ".html");
				FileWriter writer3 = new FileWriter(file3);
				writer3.append(tocReference.getTitle() + "\n");
				Resource resource = resources.getByHref(tocReference.getCompleteHref());
				writer3.append("title:" + resource.getTitle() + "id:" + resource.getId() + "大小" + resource.getSize()
						+ "href:" + resource.getHref() + new String(resource.getData(), resource.getInputEncoding()));
				writer3.close();
			}*/
			//			writer3.flush();

			/*File file = new File(curPath + "\\tempBook.html");
			FileWriter writer = new FileWriter(file);
			
			File file2 = new File(curPath + "\\tempBook2.html");
			FileWriter writer2 = new FileWriter(file2);
			//			Spine spine = book.getSpine();
			//			List<SpineReference> spineReferences = spine.getSpineReferences();
			if (spineReferences != null && spineReferences.size() > 0) {
				for (int i = 0; i < spineReferences.size(); i++) {
					SpineReference spineReference = spineReferences.get(i);
					Resource resource = spineReference.getResource();
					byte[] data = resource.getData();
					String strHtml = StrUtil.bytes2Hex(data);//章节正文
					writer.append(strHtml);
					//					System.out.println("initView: strHtml= " + strHtml);
					if (i == 0) {
						break;
					}
				}
			}
			
			writer.close();
			
			List<Resource> contents = book.getContents();
			if (contents != null && contents.size() > 0) {
				for (int i = 0; i < contents.size(); i++) {
					Resource resource = contents.get(i);
					byte[] data = resource.getData();
					String strHtml = StrUtil.bytes2Hex(data);//章节正文
					writer2.append(strHtml);
					if (i == 1) {
						break;
					}
				}
			
			}
			
			writer2.close();*/
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

}
